package com.ifast.check.Consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ifast.asset.util.HttpUtil;
import com.ifast.check.Controller.CheckTask;
import com.ifast.check.config.RabbitMQConfig;
import com.ifast.check.entity.CheckEntity;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@RabbitListener(queues = RabbitMQConfig.RABBITMQ_DEMO_TOPIC)
public class RabbitDemoConsumer {

    @Value("${auditUrl}")
    String auditUrl;
    public  volatile int taskNum = 0;
    //定义消费者   当taskNum小于15的时候就会触发  添加任务 如果不小于10  则执行等待
    @RabbitHandler
    public void process(Map checkEntitymap){

        CheckEntity checkEntity = new CheckEntity(checkEntitymap.get("msg").toString());
        //System.out.println(JSON.toJSONString(checkEntitymap));
        while(taskNum > 14){
            try{
                Thread.sleep(1000);
            }catch (Exception e){
            }
        }
        //发送何查信息
        for (int i = 0 ; i < checkEntity.getHosts().length;i++){
            String targetid = "";
            //先验证是否存在此IP的查询目标
            try {
                //获取targetId扫描任务的targetid
                String getTagetUrl = auditUrl + "/getTargetIdByIp/" + checkEntity.getHosts()[i];
                String getTargetRes = HttpUtil.sendGet(getTagetUrl, "", CheckTask.token);
                JSONObject resJson = (JSONObject) JSONObject.parse(getTargetRes);
                targetid = resJson.get("data").toString();
            } catch (Exception e) {
                //return Result.ok("creat error");
            }
            if("".equals(targetid)){
                //先添加扫描对象  在添加 扫描任务
                Map<String, Object> map = new HashMap<>();
                map.put("hosts", checkEntity.getHosts()[i]);
                map.put("name", checkEntity.getHosts()[i]);
                map.put("portId", CheckTask.portid);
                String url = auditUrl + "/createTarget";
                String res = HttpUtil.sendPost(url, JSON.toJSONString(map), CheckTask.token);
                if ("".equals(res)) {
                    return;
                }
            }
            try {
                //获取targetId扫描任务的targetid
                String getTagetUrl = auditUrl + "/getTargetIdByIp/" + checkEntity.getHosts()[i];
                String getTargetRes = HttpUtil.sendGet(getTagetUrl, "", CheckTask.token);
                JSONObject resJson = (JSONObject) JSONObject.parse(getTargetRes);
                targetid = resJson.get("data").toString();
            } catch (Exception e) {
                return ;
            }
            //继续执行添加扫描任务
            Map<String, Object> mapTask = new HashMap<>();
            mapTask.put("type", checkEntity.getType());
            mapTask.put("cron", checkEntity.getCron());
            mapTask.put("name", checkEntitymap.get("sendTime").toString()+":"+checkEntity.getName()+checkEntity.getHosts()[0]);
            mapTask.put("targetId", targetid);
            String urlTask = auditUrl + "/createTask";
            HttpUtil.sendPost(urlTask, JSON.toJSONString(mapTask), CheckTask.token);
        }
    }

    //@Scheduled(cron="0/2 * *  * * ? ")
    public void getTaskNum(){

        if ("".equals(CheckTask.token)){
            return;
        }
        //发送请求 获取
        String url = auditUrl + "/getTasksNum";
        String res = HttpUtil.sendPost(url, "", CheckTask.token);
        HashMap hashMap = JSON.parseObject(res, HashMap.class);
        String num =hashMap.get("data").toString();
        taskNum = Integer.valueOf(num);
    }
}
